/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.model;

import be.gnx.dukono.coordinate.Geographic3DCoordinate;

public class Coordinate extends Geographic3DCoordinate {

  public Coordinate(Coordinate coordinate) {
    this(coordinate.getLatitude(), coordinate.getLongitude(), coordinate.getElevation());
  }

  public Coordinate(double latitude, double longitude, double elevation) {
    super();

    while (longitude < (-Math.PI)) {
      longitude += (2 * Math.PI);
    }
    while (longitude > Math.PI) {
      longitude -= (2 * Math.PI);
    }

    while (latitude < (-(Math.PI / 2))) {
      latitude += Math.PI;
    }
    while (latitude > (Math.PI / 2)) {
      latitude -= Math.PI;
    }

    setCoordinates(latitude, longitude, elevation);
  }

  public double distance(Coordinate coordinate) {
    if (coordinate != null) {
      double longitudeDifference = getLongitude() - coordinate.getLongitude();
      double latitudeDifference = getLatitude() - coordinate.getLatitude();
      return Math.sqrt(Math.pow(longitudeDifference, 2.0) + Math.pow(latitudeDifference, 2.0));
    }
    return Double.NaN;
  }

}
